﻿<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
    <meta name="author" content="火星科技 http://mars2d.cn " />
    <meta name="apple-touch-fullscreen" content="yes" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="x5-fullscreen" content="true" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
    <!-- 标题及搜索关键字 -->
    <meta name="keywords" content="火星科技,cesium,3D,GIS,marsgis,三维,地球,地图,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
    <meta
      name="description"
      content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 三维 地球 模型  gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包  地图离线 二维地图 三维地图 全景漫游 地理信息系统 云GIS 三维GIS GIS平台 WebGIS"
    />

    <link rel="shortcut icon" type="image/x-icon" href="https://muyao1987.gitee.io/cdn/marsgis.cn/favicon/favicon.ico" />
    <title>功能示例 | Mars3D | 三维地图 | 火星科技 | 合肥火星科技有限公司</title>

    <!--第三方lib-->
    <script
      type="text/javascript"
      src="../lib/include-lib.js"
      libpath="../lib/"
      include="jquery,font-awesome,ztree,jquery.minicolors,jedate,bootstrap,bootstrap-table,bootstrap-checkbox,bootstrap-slider,layer,haoutil,toastr,mars2d,font-awesome,localforage"
    ></script>

    <link href="css/style.css" rel="stylesheet" />
  </head>

  <body>
    <!--加载前进行操作提示，优化用户体验-->
    <div id="mask" class="signmask" onclick="removeMask()"></div>

    <div id="mars3dContainer" class="mars2d-container"></div>

    <!-- 面板 -->
    <div id="toolbar" class="infoview">
      <table class="mars-table">
        <tbody>
          <tr>
            <td>简单文本：</td>
            <td>
              <input id="txtUrl" type="text" value="" required class="form-control" placeholder="请输入" />
            </td>
          </tr>

          <tr>
            <td>下拉框：</td>
            <td>
              <select id="txtCrs" class="selectpicker form-control" onchange="dataUpdate()">
                <option value="" selected="selected">默认</option>
                <option value="EPSG:3857">火星</option>
                <option value="EPSG:4326">地球</option>
                <option value="EPSG:4490">太阳</option>
              </select>
            </td>
          </tr>

          <tr>
            <td class="nametd">日期：</td>
            <td>
              <input type="text" class="form-control" id="txtStartTime" placeholder="YYYY-MM-DD" />
            </td>
          </tr>

          <tr>
            <td>滑动轨：</td>
            <td class="slider">
              <input id="slider_opacity" />
            </td>
          </tr>

          <tr>
            <td>双滑轨：</td>
            <td class="slider">
              <input id="slider_showLevel" type="text" />
            </td>
          </tr>

          <tr>
            <td>地图交互：</td>
            <td>
              <input type="text" name="" id="txtRectangle" class="form-control" value="" style="width: 150px" readonly />
              <input id="btnStartDraw" type="button" class="btn btn-primary" value="绘制" onclick="onDrawExtent()" />
              <input id="btnStartClear" type="button" class="btn btn-primary" value="清除" onclick="onClearExtent()" />
            </td>
          </tr>

          <tr>
            <td>多选：</td>
            <td class="slider">
              <div class="checkbox checkbox-primary checkbox-inline">
                <input id="showGoHome" class="styled" type="checkbox" />
                <label for="showGoHome"> 火星 </label>
              </div>

              <div class="checkbox checkbox-primary checkbox-inline">
                <input id="showSceneModePicker" class="styled" type="checkbox" />
                <label for="showSceneModePicker"> 地球 </label>
              </div>

              <div class="checkbox checkbox-primary checkbox-inline">
                <input id="showBaseLayerPicker" class="styled" type="checkbox" />
                <label for="showBaseLayerPicker"> 太阳 </label>
              </div>
            </td>
          </tr>

          <tr>
            <td>单选：</td>
            <td>
              <div class="radio radio-info radio-inline">
                <input type="radio" id="shadingMaterials1" name="shadingMaterials" onclick="chooseStar()" checked />
                <label for="shadingMaterials1">火星</label>
              </div>
              <div class="radio radio-info radio-inline">
                <input type="radio" id="shadingMaterials2" name="shadingMaterials" onclick="chooseStar()" />
                <label for="shadingMaterials2">地球</label>
              </div>
              <div class="radio radio-info radio-inline">
                <input type="radio" id="shadingMaterials3" name="shadingMaterials" onclick="chooseStar()" />
                <label for="shadingMaterials3">太阳</label>
              </div>
            </td>
          </tr>
          <tr>
            <td>颜色：</td>
            <td colspan="2">
              <input type="text" id="txtColor" class="form-control" style="width: 100px" value="#FF0000" />
            </td>
          </tr>
          <tr>
            <td id="tdViewAdd" colspan="2" style="text-align: center">
              <input type="button" class="btn btn-primary" value="错误信息" onclick="messageOut()" />
              <input type="button" class="btn btn-primary" value="警告信息" onclick="Warning()" />
              <input type="button" class="btn btn-primary" value="提示信息" onclick="Alert()" />
            </td>
          </tr>
          <tr>
            <td colspan="2">
              <!-- tree树控件 -->
              <ul id="treeOverlays" class="ztree"></ul>
            </td>
          </tr>
          <tr>
            <td colspan="2">
              <!-- table表格控件 -->
              <table id="listTable"></table>
            </td>
          </tr>
        </tbody>
      </table>
    </div>

    <script src="./js/common.js"></script>
    <script type="text/javascript">
      "use script"; //开发环境建议开启严格模式

      var map;
      let $table;
      let tileLayer;

      function initMap(options) {
        //合并属性参数，可覆盖config.json中的对应配置
        let mapOptions = mars2d.Util.merge(options, {
          center: { lat: 31.841977, lng: 117.141788 },
        });

        //创建三维地球场景
        map = new mars2d.Map("mars3dContainer", mapOptions);

        initUI();
        initTree();

        //获取台风列表
        queryTyphoonList().then(function (arr) {
          showTyphoonTable(arr);
        });

        $("#slider_showLevel").slider({
          id: "slider-showLevel-internal",
          ticks: [0, 5, 10, 15, 18, 21],
          ticks_positions: [0, 20, 40, 60, 80, 100],
          ticks_labels: ["0", "5", "10", "15", "18", "21"],
          range: true,
          value: [0, 21],
        });

        $("#slider_opacity")
          .slider({
            min: 1,
            max: 18,
            value: map.getZoom(),
          })
          .on("change", function (e) {
            if (e && e.value) {
              map.setOptions({ zoom: e.value.newValue });
            }
          });
        $("#txtColor").minicolors({
          position: "bottom left",
          control: "saturation",
        });
      }

      function fireStar() {
        if ($("#showGoHome").is(":checked")) {
          toastr.info("火星被选中了");
        }
      }

      function earthStar() {
        if ($("#showSceneModePicker").is(":checked")) {
          toastr.info("地球被选中了");
        }
      }

      function sunStar() {
        if ($("#showBaseLayerPicker").is(":checked")) {
          toastr.info("太阳被选中了");
        }
      }

      function chooseStar() {
        if ($("#shadingMaterials1").is(":checked")) {
          toastr.info("火星被选中了");
        } else if ($("#shadingMaterials2").is(":checked")) {
          toastr.info("地球被选中了");
        } else if ($("#shadingMaterials3").is(":checked")) {
          toastr.info("太阳被选中了");
        }
      }

      function messageOut() {
        window.toastr.error("展示错误信息");
      }
      function Alert() {
        toastr.info("展示提示信息");
      }
      function Warning() {
        toastr.warning("展示警告信息！");
      }
      function initUI() {
        jeDate("#txtStartTime", {
          theme: {
            bgcolor: "#135b91",
            pnColor: "#00CCFF",
          },
          format: "YYYY-MM-DD hh:mm:ss",
          zIndex: 20210825,
        });
        let now = new Date();
        $("#txtStartTime").val(now.format("yyyy-MM-dd HH:mm:ss"));
      }

      // 瓦片数据的矩形区域范围
      function onDrawExtent() {
        if (tileLayer) {
          tileLayer.rectangle = null;
        }
        map.graphicLayer.clear();
        map.graphicLayer.startDraw({
          type: "rectangle",
          style: {
            fill: false,
            outline: true,
            outlineWidth: 3,
            outlineColor: "#00bceb",
          },
          success: function (graphic) {
            $("#txtRectangle").val(JSON.stringify(graphic.coordinates));
          },
        });
      }

      //清除图层限定范围
      function onClearExtent() {
        $("#txtRectangle").val("");
        map.graphicLayer.clear();

        if (tileLayer) {
          tileLayer.rectangle = null;
          tileLayer.options.flyTo = false;
          tileLayer.reload();
        }
      }

      //显示台风列表
      function showTyphoonTable(data) {
        $("#listTable").bootstrapTable({
          data: data,
          height: 300,
          pagination: false,
          singleSelect: false,
          checkboxHeader: false,
          columns: [
            {
              title: "是否显示",
              field: "show",
              align: "center",
              checkbox: true,
              with: 50,
            },
            {
              title: "台风编号",
              field: "typnumber",
              align: "center",
            },
            {
              title: "台风名（中文）",
              field: "name_cn",
              align: "center",
            },
            {
              title: "台风名（英文）",
              field: "name_en",
              align: "center",
            },
          ],
          onCheck: function (row) {
            selectOneTyphoon(row);
          },
          onClickRow: function (row) {
            if (typhoonListObj[row.id] && typhoonListObj[row.id].show) {
              selectOneTyphoon(row);
            }
          },
        });
      }

      let typhoonListObj = {};

      //勾选了台风
      function selectOneTyphoon(row) {
        $("#lblName").html("[" + row.typnumber + "]" + "" + row.name_cn);

        toastr.info(row.name_cn + "台风要来啦，大家要注意安全！");
      }

      //访问后端接口，取台风列表数据
      function queryTyphoonList() {
        return new Promise(function (resolve, reject) {
          function typhoon_jsons_list_default(data) {
            let arr = [];
            data.typhoonList.forEach((item) => {
              arr.push({
                id: item[0],
                name_en: item[1],
                name_cn: item[2],
                typnumber: item[3],
                state: item[7],
              });
            });
            resolve(arr);
          }
          window.typhoon_jsons_list_default = typhoon_jsons_list_default;

          $.ajax({
            url: "//data.mars2d.cn/file/apidemo/typhoon/list_2020.json", //mock离线数据
            dataType: "json",
            type: "GET",
            data: {
              t: new Date().getTime(),
            },
            success: typhoon_jsons_list_default,
            error: function (data) {
              console.log("台风列表数据获取失败", data);
              // haoutil.msg('台风列表数据获取失败！')
              reject(data);
            },
          });
        });
      }

      //===========================树控件处理============================
      let layers = [];
      let layersObj = {};

      function initTree() {
        //初始化树
        let setting = {
          check: {
            enable: true,
          },
          data: {
            simpleData: {
              enable: true,
            },
          },
          callback: {
            onCheck: treeOverlays_onCheck,
          },
        };

        let zNodes = [];
        layers = map.getLayers({
          basemaps: true, //是否取config.json中的basempas
          layers: true, //是否取config.json中的layers
        });

        //遍历出config.json中所有的basempas和layers
        for (let i = layers.length - 1; i >= 0; i--) {
          let node = _getNodeConfig(layers[i]);
          if (node) {
            zNodes.push(node);
          }
        }

        //
        $.fn.zTree.init($("#treeOverlays"), setting, zNodes);
      }

      function _getNodeConfig(layer) {
        if (layer == null || layer.options.noLayerManage) {
          return;
        }

        let item = layer.options;

        if (item.name == "未命名") {
          return;
        }

        let node = {
          id: layer.id,
          pId: layer.pid,
          name: layer.name,
          uuid: layer.uuid,
        };

        if (layer.hasEmptyGroup) {
          //空数组
          node.icon = "img/layer/folder.png";
          node.open = item.open == null ? true : item.open;
          layersObj[node.uuid] = layer;
        } else if (layer.hasChildLayer) {
          //有子节点的数组
          node.icon = "img/layer/layerGroup.png";
          node.open = item.open == null ? true : item.open;
          layersObj[node.uuid] = layer;
        } else {
          node.icon = "img/layer/layer.png";
          node.checked = layer.isAdded && layer.show;

          if (layer.parent) {
            node._parentId = layer.parent.uuid;
          }
          //记录图层
          layersObj[node.uuid] = layer;
        }
        return node;
      }
      function treeOverlays_onCheck(e, treeId, chktreeNode) {
        let treeObj = $.fn.zTree.getZTreeObj(treeId);
        //获得所有改变check状态的节点
        let changedNodes = treeObj.getChangeCheckedNodes();

        haoutil.array.remove(changedNodes, chktreeNode);
        changedNodes.push(chktreeNode);

        for (let i = changedNodes.length - 1; i >= 0; i--) {
          let treeNode = changedNodes[i];
          treeNode.checkedOld = treeNode.checked;

          if (treeNode.check_Child_State == 1) {
            // 0:无子节点被勾选,  1:部分子节点被勾选,  2:全部子节点被勾选, -1:不存在子节点 或 子节点全部设置为 nocheck = true
            continue;
          }

          let layer = layersObj[treeNode.uuid];
          if (layer == null) {
            continue;
          }

          //显示隐藏透明度设置view
          if (treeNode.checked) {
            $("#" + treeNode.tId + "_range").show();
          } else {
            $("#" + treeNode.tId + "_range").hide();
          }
          updateLayerShow(layer, treeNode.checked);
        }
      }

      //更新图层:显示隐藏状态
      function updateLayerShow(layer, show) {
        if (show) {
          if (!layer.isAdded) {
            map.addLayer(layer);
          }
        } else {
          if (layer.isAdded) {
            map.removeLayer(layer);
          }
        }
      }
    </script>
  </body>
</html>
